package com.longdaji.decoration.utils;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.longdaji.decoration.R;

/**
 * Created by Kanade on 2018/2/25 0025.
 */

public class BottomDialog extends Dialog {
    private TextView tvOne;
    private TextView tvTwo;
    private TextView tvThree;
    private TextView tvCancel;
    private View divisionOne;
    private View divisionTwo;
    private String mItemNameOne;
    private String mItemNameTwo;
    private String mItemNameThree;

    private Context mContext;
    private BottomDialogListener mBottomDialogListener = null;

    public BottomDialog(Context context, String itemNameOne) {
        this(context, itemNameOne, null);
    }

    public BottomDialog(Context context, String itemNameOne, String itemNameTwo) {
        this(context, itemNameOne, itemNameTwo, null);
    }

    public BottomDialog(Context context, String itemNameOne, String itemNameTwo, String itemNameThree) {
        super(context, R.style.BottomDialogTheme);
        mContext = context;
        mItemNameOne = itemNameOne;
        mItemNameTwo = itemNameTwo;
        mItemNameThree = itemNameThree;
        initView();

    }

    private void initView() {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.dialog_bottom, null);
        tvOne = view.findViewById(R.id.tv_item1);
        tvTwo = view.findViewById(R.id.tv_item2);
        tvThree = view.findViewById(R.id.tv_item3);
        tvCancel = view.findViewById(R.id.tv_cancel);
        divisionOne = view.findViewById(R.id.bottom_division1);
        divisionTwo = view.findViewById(R.id.bottom_division2);

        tvOne.setOnClickListener(new BottomClick());
        tvTwo.setOnClickListener(new BottomClick());
        tvThree.setOnClickListener(new BottomClick());
        tvCancel.setOnClickListener(new BottomClick());

        tvOne.setText(mItemNameOne);
        tvTwo.setText(mItemNameTwo);
        tvThree.setText(mItemNameThree);
        tvCancel.setText("取消");

        if (null == mItemNameOne) {
            Log.e("BottomDialog", "item数目为0");
            return;
        } else if (null == mItemNameTwo) {
            tvTwo.setVisibility(View.GONE);
            tvThree.setVisibility(View.GONE);
            divisionOne.setVisibility(View.GONE);
            divisionTwo.setVisibility(View.GONE);
        } else if (null == mItemNameThree) {
            tvThree.setVisibility(View.GONE);
            divisionTwo.setVisibility(View.GONE);
        }


        setContentView(view);
        //这里必须要放在setContentView后面，不然MATCH_PARENT无效
        Window window = getWindow();
        if (window != null) {
            window.setGravity(Gravity.BOTTOM);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.x = 0;
            lp.y = 0;
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);
        }

    }

    private class BottomClick implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.tv_item1:
                    mBottomDialogListener.onItemOneClick();
                    dismiss();
                    break;
                case R.id.tv_item2:
                    mBottomDialogListener.onItemTwoClick();
                    dismiss();
                    break;
                case R.id.tv_item3:
                    mBottomDialogListener.onItemThreeClick();
                    dismiss();
                    break;
                case R.id.tv_cancel:
                    dismiss();
                    break;
                default:
                    break;
            }
        }
    }

    public interface BottomDialogListener {
        void onItemOneClick();

        void onItemTwoClick();

        void onItemThreeClick();
    }

    public void setBottomDialogListener(BottomDialogListener listener) {
        mBottomDialogListener = listener;
    }

}
